package com.css.dzj.dzjportal.modules.complaint.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.ServiceUtil;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.complaint.entity.JgWgrTsjbglEntity;
import com.css.dzj.dzjportal.modules.complaint.service.CompaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/compaint")
@PropertySource(value = "classpath:base.properties")
public class CompaintController {
    @Value("${WGR_URL}")
    private String WGR_URL;
    @Autowired
    private CompaintService compaintService;

    /**
     * 投诉举报新增
     * @param jgWgrTsjbglEntity
     * @return
     */
    @PostMapping("/add")
    public R addCompaint(@RequestBody JgWgrTsjbglEntity jgWgrTsjbglEntity){
        String url=WGR_URL+"/appeal/addAppeal";
        String jgWgr= JSON.toJSONString(jgWgrTsjbglEntity);
        String msg= HttpClientUtil.doPostJson(url,jgWgr);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
          return R.ok();
        }else {
          return R.error("添加失败");
        }
    }

    /**
     * 投诉举报列表
     * @param pageSize
     * @param pageNo
     * @param name
     * @param appealStatus
     * @param appealContent
     * @param startTime
     * @param endTime
     * @param request
     * @return
     */
    @GetMapping("/select")
    public R selectPage(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                        @RequestParam(name = "pageNo" ,required = false)Integer pageNo,
                        @RequestParam(name = "name" ,required = false)String name,
                        @RequestParam(name = "appealStatus" ,required = false)String appealStatus,
                        @RequestParam(name = "appealContent" ,required = false)String appealContent,
                        @RequestParam(name = "startTime" ,required = false)String startTime,
                        @RequestParam(name = "endTime" ,required = false)String endTime,
                        HttpServletRequest request) {
        String loginName= ServiceUtil.getLoginName(request);
        String url=WGR_URL+"/appeal/getPageName";
        HashMap<String,String> map=new HashMap<>();
        map.put("pageSize",pageSize.toString());
        map.put("pageNo",pageNo.toString());
        map.put("name",name);
        map.put("appealStatus",appealStatus);
        map.put("appealContent",appealContent);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("loginName",loginName);
        String msg= HttpClientUtil.doGet(url,map);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
            return R.ok().put("data",jsonObject.get("data"));
        }else {
            return R.error("查询失败");
        }
    }

    /**
     * 投诉举报详情
     * @param id
     * @return
     */
    @GetMapping("/getappeal")
    public R getAppeal(@RequestParam String id) {
        String url=WGR_URL+"/appeal/getOne";
        HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        String msg= HttpClientUtil.doGet(url,map);
        JSONObject jsonObject=(JSONObject) JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
            return R.ok().put("data",jsonObject.get("data"));
        }else {
            return R.error("查询失败");
        }

    }
    /**
     * 投诉举报暂存
     * @param jgWgrTsjbglEntity
     * @return
     */
    @PostMapping("/zcAppeal")
    public R zcCompaint(@RequestBody JgWgrTsjbglEntity jgWgrTsjbglEntity){
        String url=WGR_URL+"/appeal/zcAppeal";
        String jgWgr= JSON.toJSONString(jgWgrTsjbglEntity);
        String msg= HttpClientUtil.doPostJson(url,jgWgr);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
            return R.ok();
        }else {
            return R.error("暂存失败");
        }
    }
    /**
     * 投诉举报提交
     * @param id
     * @return
     */
    @PostMapping("/tjAppeal")
    public R tjCompaint(String id){
        String url=WGR_URL+"/appeal/tjAppeal";
        HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        String msg= HttpClientUtil.doPost(url,map);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
            return R.ok();
        }else {
            return R.error("提交失败");
        }
    }
    /**
     * 投诉举报修改
     * @param jgWgrTsjbglEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody JgWgrTsjbglEntity jgWgrTsjbglEntity){
        String url=WGR_URL+"/appeal/update";
        String jgWgr= JSON.toJSONString(jgWgrTsjbglEntity);
        String msg= HttpClientUtil.doPostJson(url,jgWgr);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
            return R.ok();
        }else {
            return R.error("暂存失败");
        }
    }
    /**
     * 投诉举报列表(监管数据仓)
     * @param pageSize
     * @param pageNo
     * @param name
     * @param appealStatus
     * @param records
     * @param startTime
     * @param endTime
     * @param request
     * @return
     */
    @GetMapping("/selectAllPage")
    public R selectAllPage(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                           @RequestParam(name = "pageNo" ,required = false)Integer pageNo,
                           @RequestParam(name = "name" ,required = false)String name,
                           @RequestParam(name = "appealStatus" ,required = false)String appealStatus,
                           @RequestParam(name = "records" ,required = false)String records,
                           @RequestParam(name = "startTime" ,required = false)String startTime,
                           @RequestParam(name = "endTime" ,required = false)String endTime,
                           HttpServletRequest request) {
        String url=WGR_URL+"/appeal/getPage";
        HashMap<String,String> map=new HashMap<>();
        map.put("pageSize",pageSize.toString());
        map.put("pageNo",pageNo.toString());
        map.put("name",name);
        map.put("appealStatus",appealStatus);
        map.put("records",records);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        String msg= HttpClientUtil.doGet(url,map);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject!=null&&jsonObject.get("code").equals(200)){
            return R.ok().put("data",jsonObject.get("data"));
        }else {
            return R.error("查询失败");
        }
    }

    /**
     * 导出选中数据
     * @param response
     * @param ids
     * @return
     */
    @GetMapping("/exportByIds")
    public R exportAppealToExcel(HttpServletResponse response, @RequestParam String ids) {
        try {
            String url=WGR_URL+"/appeal/queryByIds";
            HashMap<String,String> map=new HashMap<>();
            map.put("ids",ids);
            String msg= HttpClientUtil.doGet(url,map);
            JSONObject jsonObject=(JSONObject)JSON.parse(msg);
            if(jsonObject!=null&&jsonObject.get("code").equals(200)){
                String JSONStr = JSON.toJSONString(jsonObject.get("data"));
                List<JgWgrTsjbglEntity> jgWgrTsjbglEntities = JSON.parseArray(JSONStr,JgWgrTsjbglEntity.class);
                compaintService.exportAppealToExcel(response, jgWgrTsjbglEntities);
            }else {
                return R.error("无数据");
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 批量导出
     * @param response
     * @param name
     * @param appealStatus
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam(name = "name" ,required = false)String name,
                    @RequestParam(name = "appealStatus" ,required = false)String appealStatus,
                    @RequestParam(name = "startTime" ,required = false)String startTime,
                    @RequestParam(name = "endTime" ,required = false)String endTime) {
        try {

            String url=WGR_URL+"/appeal/queryByParams";
            HashMap<String,String> map=new HashMap<>();
            map.put("name",name);
            map.put("appealStatus",appealStatus);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            String msg= HttpClientUtil.doGet(url,map);
            JSONObject jsonObject=(JSONObject)JSON.parse(msg);
            if(jsonObject!=null&&jsonObject.get("code").equals(200)){
                String JSONStr = JSON.toJSONString(jsonObject.get("data"));
                List<JgWgrTsjbglEntity> jgWgrTsjbglEntities = JSON.parseArray(JSONStr,JgWgrTsjbglEntity.class);
                compaintService.exportAppealToExcel(response, jgWgrTsjbglEntities);
            }else {
                return R.error("无数据");
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    /**
     * 查询投诉举报个数
     */
    @GetMapping("/getAppealCount")
    public R getAppealCount(){
        try{
            String url=WGR_URL+"/appeal/getAppealCount";
            String msg= HttpClientUtil.doGet(url);
            JSONObject jsonObject=(JSONObject)JSON.parse(msg);
            if(!StringUtils.isEmpty(msg)){
                return R.ok().put("data",jsonObject.get("data"));
            }
            return R.ok().put("data",0);
        }catch (Exception e){
            return R.error(e.getMessage());
        }

    }
}
